package com.zline.foodraise.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.photoselector.util.CommonUtils;
import com.zline.foodraise.R;
import com.zline.foodraise.entity.TemplateBean;
import com.zline.foodraise.util.ImageUtil;
import com.zline.foodraise.view.TemplateProcessThumbItem;
import com.zline.foodraise.view.TemplateProcessThumbItem_;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

/**
 * Created by heshantao on 15/9/16.
 * 模板处理中项目模板缩略图列表
 */
@EBean
public class ProjectTemplateThumbListAdapter extends RecyclerViewBaseAdapter<TemplateBean, TemplateProcessThumbItem> {
    private static final String LOG_TAG = ProjectTemplateThumbListAdapter.class.getSimpleName();
    @RootContext
    Context cxt;
    private int itemWidth;
    private int horizentalNum = 4;
    private RelativeLayout.LayoutParams itemLayoutParams;
    long mtemplateId;
    DisplayImageOptions options;
    @Bean
    ImageUtil imageUtil;

    @Override
    protected TemplateProcessThumbItem onCreateItemView(ViewGroup parent, int viewType) {
        TemplateProcessThumbItem thumbItem = TemplateProcessThumbItem_.build(cxt);
        setItemWidth();
        thumbItem.setLayoutParams(itemLayoutParams);
        return thumbItem;
    }

    /**
     * 设置每一个Item的宽高
     */
    public void setItemWidth() {
        int screenWidth = CommonUtils.getDeviceWidth(cxt);
        int horizentalSpace = cxt.getResources().getDimensionPixelSize(com.photoselector.R.dimen.sticky_item_horizontalSpacing);
        this.itemWidth = (screenWidth - (horizentalSpace * (horizentalNum - 1))) / horizentalNum;
        this.itemLayoutParams = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
    }

    public void setSelectedTemplate(long templateId) {
        mtemplateId = templateId;
    }

    @Override
    public void onBindViewHolder(ViewWrapper<TemplateProcessThumbItem> holder, int position) {
        TemplateProcessThumbItem item = holder.getView();
        TemplateBean bean = arrys.get(position);
        if (options == null) {
            options = imageUtil.
                    getDisplayImageOptions(R.mipmap.ic_picture_loading,
                            R.mipmap.ic_picture_loadfailed, R.mipmap.ic_picture_loadfailed);
        }
        item.bind(position, mtemplateId, bean,options);
        item.setRecycleItemClickListener(itemClicked);
    }
}
